﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Helix_Network_Supervisor_3
{
    class ApplicationSettings
    {
        #region Fields

        private StatusForm statusForm = new StatusForm();
        private bool _setupSuccessful = false;

        #endregion Fields

        #region Properties

        public string showFilePath
        {
            get { return Properties.Settings.Default.showFilePath; }
            set { Properties.Settings.Default.showFilePath = value; }
        }

        public string scheduleFilePath
        {
            get { return Properties.Settings.Default.scheduleFilePath; }
            set { Properties.Settings.Default.scheduleFilePath = value; }
        }

        public string playlistFilePath
        {
            get { return Properties.Settings.Default.playlistFilePath; }
            set { Properties.Settings.Default.playlistFilePath = value; }
        }

        public string channelMapFilePath
        {
            get { return Properties.Settings.Default.channelMapFilePath; }
            set { Properties.Settings.Default.channelMapFilePath = value; }
        }

        public string uploadFilePath
        {
            get { return Properties.Settings.Default.uploadFilePath; }
            set { Properties.Settings.Default.uploadFilePath = value; }
            //get { return _uploadFilePath; }
            //set { _uploadFilePath = value; }
        }

        public string songPathDirectory
        {
            get { return Properties.Settings.Default.songPathDirectory; }
            set { Properties.Settings.Default.songPathDirectory = value; }
        }

        public string vixenV2ProfilePath
        {
            get { return Properties.Settings.Default.vixenV2ProfilePath; }
            set { Properties.Settings.Default.vixenV2ProfilePath = value; }
        }

        public string vixenV2AudioPath
        {
            get { return Properties.Settings.Default.vixenV2AudioPath; }
            set { Properties.Settings.Default.vixenV2AudioPath = value; }
        }

        public string vixenSeqFilePath
        {
            get { return Properties.Settings.Default.vixenSeqFilePath; }
            set { Properties.Settings.Default.vixenSeqFilePath = value; }
        }

        public string mp3ChipVersion
        {
            get { return Properties.Settings.Default.mp3ChipVer; }
            set { Properties.Settings.Default.mp3ChipVer = value; }
        }

        public string band0Name
        {
            get { return Properties.Settings.Default.band0Name; }
            set { Properties.Settings.Default.band0Name = value; }
        }

        public string band1Name
        {
            get { return Properties.Settings.Default.band1Name; }
            set { Properties.Settings.Default.band1Name = value; }
        }

        public string band2Name
        {
            get { return Properties.Settings.Default.band2Name; }
            set { Properties.Settings.Default.band2Name = value; }
        }

        public string band3Name
        {
            get { return Properties.Settings.Default.band3Name; }
            set { Properties.Settings.Default.band3Name = value; }
        }

        public string band4Name
        {
            get { return Properties.Settings.Default.band4Name; }
            set { Properties.Settings.Default.band4Name = value; }
        }

        public string band5Name
        {
            get { return Properties.Settings.Default.band5Name; }
            set { Properties.Settings.Default.band5Name = value; }
        }

        public int numberVisibleButtons
        {
            get { return Properties.Settings.Default.numberVisibleButtons; }
            set { Properties.Settings.Default.numberVisibleButtons = value; }
        }

        public bool setupSuccessful
        {
            get { return _setupSuccessful; }
        }

        public string PauseName0
        {
            get { return Properties.Settings.Default.pauseName0; }
                
            set { Properties.Settings.Default.pauseLength0 = value; }
        }

        public string PauseLength0
        {
            get { return Properties.Settings.Default.pauseLength0; }
            set { Properties.Settings.Default.pauseLength0 = value; }
        }

        public string PauseName1
        {
            get { return Properties.Settings.Default.pauseName1; }
            set { Properties.Settings.Default.pauseName1 = value; }
        }

        public string PauseLength1
        {
            get { return Properties.Settings.Default.pauseLength1; }
            set { Properties.Settings.Default.pauseLength1 = value; }
        }

        public string PauseName2
        {
            get { return Properties.Settings.Default.pauseName2; }
            set { Properties.Settings.Default.pauseName2 = value; }
        }

        public string PauseLength2
        {
            get { return Properties.Settings.Default.pauseLength2; }
            set { Properties.Settings.Default.pauseLength2 = value; }
        }

        public string PauseName3
        {
            get { return Properties.Settings.Default.pauseName3; }
            set { Properties.Settings.Default.pauseName3 = value; }
        }

        public string PauseLength3
        {
            get { return Properties.Settings.Default.pauseLength3; }
            set { Properties.Settings.Default.pauseLength3 = value; }
        }

        public string PauseName4
        {
            get { return Properties.Settings.Default.pauseName4; }
            set { Properties.Settings.Default.pauseName4 = value; }
        }

        public string PauseLength4
        {
            get { return Properties.Settings.Default.pauseLength4; }
            set { Properties.Settings.Default.pauseLength4 = value; }
        }

        public string PauseName5
        {
            get { return Properties.Settings.Default.pauseName5; }
            set { Properties.Settings.Default.pauseName5 = value; }
        }

        public string PauseLength5
        {
            get { return Properties.Settings.Default.pauseLength5; }
            set { Properties.Settings.Default.pauseLength5 = value; }
        }

        public int BaudRate
        {
            get { return Properties.Settings.Default.baudRate; }
            set { Properties.Settings.Default.baudRate = value; }
        }

        public int PanID
        {
            get { return Properties.Settings.Default.panID; }
            set { Properties.Settings.Default.panID = value; }
        }

        public string PortName
        {
            get { return Properties.Settings.Default.portName; }
            set { Properties.Settings.Default.portName = value; }
        }

        public string PortFriendlyName
        {
            get { return Properties.Settings.Default.portFriendlyName; }
            set { Properties.Settings.Default.portFriendlyName = value; }
        }

        #endregion Properties

        #region Ctor

        public ApplicationSettings()
        {
            OpenApplicationSettings();
        }

        #endregion Ctor

        #region Public Methods

        /// <summary>
        /// Save the Application Settings
        /// </summary>
        public void SaveApplicationSettings()
        {
            Properties.Settings.Default.Save();
        }

        /// <summary>
        /// Open the Application Settings
        /// </summary>
        public void OpenApplicationSettings()
        {
            // Check to see if ShowFilePath is blank
            // If so then run the wizard else load the settings
            if ((showFilePath == "") || (scheduleFilePath == "") || (playlistFilePath == "") ||
                (channelMapFilePath == "") || (uploadFilePath == "") || (vixenV2ProfilePath == "") ||
                (vixenV2AudioPath == "") || (vixenSeqFilePath == ""))
            {
                DialogResult dr = MessageBox.Show("Some of the settings need to be set." + "\r\rDo you want to run the Setup Wizard?",
                            "Helix Show Builder Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);

                if (dr == DialogResult.Yes)
                {
                    Run_Wizard();
                }
                else
                {
                    MessageBox.Show("The Setup was not completed." + "\r\rThe Helix Network Supervisor will not operate correctly." +
                    "\rThe Helix Network Supervisor will be shut down",
                        "Helix Show Builder Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    _setupSuccessful = false;
                }
            }
            else
            {
                _setupSuccessful = true;
            }
        }

        /// <summary>
        /// Run the Setup Wizard
        /// </summary>
        public void Run_Wizard()
        {
            Wizard wizard = new Wizard();

            if (wizard.ShowDialog() == DialogResult.OK)
            {
                showFilePath = wizard.helixPath + @"\Helix\Shows";
                scheduleFilePath = wizard.helixPath + @"\Helix\Schedules";
                playlistFilePath = wizard.helixPath + @"\Helix\Playlists";
                channelMapFilePath = wizard.helixPath + @"\Helix\Channel Maps";
                uploadFilePath = wizard.helixPath + @"\Helix\Uploads";
                songPathDirectory = wizard.audioPath;
                vixenV2ProfilePath = wizard.vixenPath + @"\Profiles";
                vixenV2AudioPath = wizard.vixenPath + @"\Audio";
                vixenSeqFilePath = wizard.vixenPath + @"\Sequences";
                mp3ChipVersion = wizard.mp3ChipVersion;

                Properties.Settings.Default.Save();

                if (!Directory.Exists(uploadFilePath))
                {
                    Directory.CreateDirectory(uploadFilePath);
                }

                Uri firmUri = new Uri("http://www.helixlightcontroller.com/downloads/NEWFIRMWARE.BIN");
                string newFilePath = uploadFilePath + @"\FIRMWARE.BIN"; ;

                // Create a new WebClient instance.
                WebClient firmWebClient = new WebClient();

                firmWebClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(firmWebClient_DownloadFileCompleted);
                firmWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(firmWebClient_DownloadProgressChanged);

                // Download the Web resource and save it into the current filesystem folder.

                statusForm.UpdateStatusTitle("Downloading Firmware File");
                statusForm.UpdateStatusLabel("Downloading NEWFIRMWARE.BIN");
                statusForm.Show();

                try
                {
                    firmWebClient.DownloadFileAsync(firmUri, newFilePath);

                }
                catch
                {
                    statusForm.Close();
                    MessageBox.Show("Unable to download the Firmware File.  Please make sure you are connected to the Internet");
                    return;
                }

                _setupSuccessful = true;
            }
            else
            {
                DialogResult dr = MessageBox.Show("The Setup Wizard was not completed." + "\r\rDo you want to run the Setup Wizard again?",
                            "Helix Show Builder Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);

                if (dr == DialogResult.Yes)
                {
                    Run_Wizard();
                }
                else
                {
                    _setupSuccessful = false;
                }
            }
        }

        public void SavePauseName(int index, string name)
        {
            switch (index)
            {
                case 0:
                    PauseName0 = name;
                    break;

                case 1:
                    PauseName1 = name;
                    break;

                case 2:
                    PauseName2 = name;
                    break;

                case 3:
                    PauseName3 = name;
                    break;

                case 4:
                    PauseName4 = name;
                    break;

                case 5:
                    PauseName5 = name;
                    break;
            }//end Switch
        }

        public void SavePauseLength(int index, string length)
        {
            switch (index)
            {
                case 0:
                    PauseLength0 = length;
                    break;

                case 1:
                    PauseLength1 = length;
                    break;

                case 2:
                    PauseLength2 = length;
                    break;

                case 3:
                    PauseLength3 = length;
                    break;

                case 4:
                    PauseLength4 = length;
                    break;

                case 5:
                    PauseLength5 = length;
                    break;
            }
        }

        #endregion Public Methods

        #region Private Methods

        private void DownloadCalibrateSeq()
        {
            Uri seqUri = new Uri("http://www.helixlightcontroller.com/downloads/CALIBRAT.SEQ");
            string newFilePath = uploadFilePath + @"\CALIBRAT.SEQ";

            // Create a new WebClient instance.
            WebClient seqWebClient = new WebClient();

            seqWebClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(seqWebClient_DownloadFileCompleted);
            seqWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(seqWebClient_DownloadProgressChanged);

            // Download the Web resource and save it into the current filesystem folder.

            statusForm = new StatusForm();

            statusForm.UpdateStatusTitle("Downloading Calibration Sequence File");
            statusForm.UpdateStatusLabel("Downloading CALIBRAT.SEQ");
            statusForm.Show();

            try
            {
                seqWebClient.DownloadFileAsync(seqUri, newFilePath);

            }
            catch
            {
                statusForm.Close();
                MessageBox.Show("Unable to download the Calibration Sequence File.  Please make sure you are connected to the Internet");
                return;
            }
        }

        private void DownloadCalibrateMP3()
        {
            Uri mp3Uri = new Uri("http://www.helixlightcontroller.com/downloads/CALIBRAT.MP3");
            string newFilePath = uploadFilePath + @"\CALIBRAT.MP3";

            // Create a new WebClient instance.
            WebClient mp3WebClient = new WebClient();

            mp3WebClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(mp3WebClient_DownloadFileCompleted);
            mp3WebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(mp3WebClient_DownloadProgressChanged);

            // Download the Web resource and save it into the current filesystem folder.

            statusForm = new StatusForm();

            statusForm.UpdateStatusTitle("Downloading Calibration MP3 File");
            statusForm.UpdateStatusLabel("Downloading CALIBRAT.MP3");
            statusForm.Show();

            try
            {
                mp3WebClient.DownloadFileAsync(mp3Uri, newFilePath);

            }
            catch
            {
                statusForm.Close();
                MessageBox.Show("Unable to download the Calibration MP3 File.  Please make sure you are connected to the Internet");
                return;
            }
        }

        #endregion Private Methods

        #region Event Handlers
        void mp3WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            statusForm.UpdateProgressBarPercent(e.ProgressPercentage);
        }

        void mp3WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            statusForm.Close();
            MessageBox.Show("Setup is complete", "Setup Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        void seqWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            statusForm.UpdateProgressBarPercent(e.ProgressPercentage);
        }

        void seqWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            statusForm.Close();
            DownloadCalibrateMP3();
        }

        void firmWebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            statusForm.UpdateProgressBarPercent(e.ProgressPercentage);
        }

        void firmWebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            statusForm.Close();
            DownloadCalibrateSeq();
        }
        #endregion Event Handlers
    }
}
